import requests
from lxml import etree
import pymongo

urls = [f'https://quotes.toscrape.com/page/{page}/' for page in range(1,2)]

clint = pymongo.MongoClient(host='localhost', port=27017)
clint.drop_database('toscrape')
db = clint['toscrape']
collection = db['toscrape']

datas = []
for url in urls:
    print(url)
    res = requests.get(url)
    tree = etree.HTML(res.text)
    items = tree.xpath('//div[@class="quote"]')
    for item in items:
        title = item.xpath('.//span[@class="text"]/text()')[0]
        author = item.xpath('.//small[@class="author"]/text()')[0]
        tags = item.xpath('.//div[@class="tags"]/a/text()')
        datas.append({'title': title, 'author': author, 'tags': tags})
        collection.insert_many(datas)
        # print(title, author, tags)


